package com.zhs.demoes;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhs.demoes.entity.Book;
import org.apache.http.HttpHost;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

/**
 * @author: zhs
 * @since: 2021/5/13 12:30
 */
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class EsTestInsert {
    RestHighLevelClient restHighLevelClient;

    @Test
    public void test(){
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200,"http")));


//        BulkRequest bulkRequest = new BulkRequest();


//        SearchRequest searchRequest = new SearchRequest();
        IndexRequest indexRequest = new IndexRequest();

        ObjectMapper objectMapper = new ObjectMapper();
        Book book = new Book();
        book.setBookId(1);
        book.setName("第一本书");
        try {
            objectMapper.writeValueAsBytes(book);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }

    @BeforeAll
    public  void before()  {
        restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200,"http")));
    }
    @Test
    public void testInsert(){

    }

    @Test
    void createIndex() throws IOException {

        // 创建索引请求,并指定索引名称
        CreateIndexRequest flower = new CreateIndexRequest("flower");

        // 执行请求
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(flower, RequestOptions.DEFAULT);

        System.out.println(createIndexResponse);
    }
    @Test
    public void testSearchAccount() throws IOException {
        SearchRequest searchRequest = new SearchRequest("account");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder termQueryBuilder = new MatchQueryBuilder("gender","M");

        searchSourceBuilder.query(termQueryBuilder);

        searchRequest.source(searchSourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = search.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }


    }
}
